﻿using System;
using System.IO;
using OpenPop.Mime;
namespace EmlViewer.EmlViewerModel
{
    public class Model
    {
        private const string TextMediaType = "text/plain";
        private const string HtmlMediaType = "text/html";
        public object Load(string fileName)
        {
            try
            {
                var fi = new FileInfo(fileName);
                Message rawMessage = Message.Load(fi);
                var result = new MessageInfo(rawMessage, fileName);
                return result;
            }
            catch (Exception ex)
            {
                return ex;
            }
        }
        public bool TryGetHtml(MessageInfo mi, out string result)
        {
            try
            {
                MessagePart mp = mi.FindFirstMessagePartWithMediaType(HtmlMediaType);
                if (mp != null)
                {
                    result = mp.GetBodyAsText();
                    return true;
                }
            }
            catch (Exception ex)
            {
                result = ex.Message;
                return false;
            }
            result = String.Empty;
            return false;
        }
        public bool TryGetText(MessageInfo mi, out string result)
        {
            try
            {
                MessagePart mp = mi.FindFirstMessagePartWithMediaType(TextMediaType);
                if (mp != null)
                {
                    result = mp.GetBodyAsText();
                    return true;
                }
            }
            catch (Exception ex)
            {
                result = ex.Message;
                return false;
            }
            result = String.Empty;
            return false;
        }
    }
}